/****************************************************************************************/
/* MediaFrame is an Open Source streaming media platform in Java                        */
/* which provides a fast, easy to implement and extremely small applet                  */
/* that enables to view your audio/video content without having                         */
/* to rely on external player applications or bulky plug-ins.                           */
/*                                                                                      */
/*--------------------------------------------------------------------------------------*/
/*                                                                                      */
/* "DataBlock.java"                                                                     */
/*                                                                                      */
/*--------------------------------------------------------------------------------------*/
/*                                                                                      */
/* We changed a lot of code and added a lot of functionality.							*/
/* This includes, but not limited to, the following changes:							*/
/* 1. The project was renamed to MediaFrame;											*/
/* 2. The connection speed detection procedure was added;								*/
/* 3. The JavaScript API functions were added;											*/
/* 4. The pre and post image support functionality was added;							*/
/* 5. The ability to save movie into the local disk was added;							*/
/* 6. The inner buffer for a movie file was added;										*/
/* 7. The click-through functionality was added;    									*/
/* 8. The .zip files support was added;    												*/
/* 9. The realtime feedback agent functionality was added.    							*/
/* For the full list of the current functionality please visit the following web page:	*/
/* http://mediaframe.org/																*/
/*    																					*/
/* 06 Jul 2002 - 19 Dec 2004 Konstantin Belous, Oleg Lebedev							*/
/*																						*/
/*--------------------------------------------------------------------------------------*/
/*                                                                                      */
/* This program is free software; you can redistribute it and/or modify it under the    */
/* terms of the GNU General Public License as published by the Free Software            */
/* Foundation; either version 2 of the License, or (at your option) any later version.  */
/*                                                                                      */
/* This program is distributed in the hope that it will be useful, but WITHOUT ANY      */
/* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      */
/* PARTICULAR PURPOSE. See the GNU General Public License for more details.             */
/*                                                                                      */
/* You should have received a copy of the GNU General Public License along with this    */
/* program; (See "LICENSE.GPL"). If not, write to the Free Software Foundation, Inc.,   */
/* 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.                            */
/*                                                                                      */
/*--------------------------------------------------------------------------------------*/
/*                                                                                      */
/* If the program runs as Java applet it isn't "interactive" in the sense of the GNU    */
/* General Public License. So paragraph 2c doesn't apply.                               */
/*                                                                                      */
/****************************************************************************************/

package mediaframe.mpeg1;

/**
 * The <code>DataBlock</code> class represents a block of 64 bytes.
 * This class contains methods for storing and reading the movie data.
 * It contains the link to the next datablock with the movie data.  
 * 
 * @author Konstantin Belous
 */
public final class DataBlock {
	
	/** The length of the block in bytes. */
	public static final int BLOCKSIZE = 64;
	/** The size if the datablock. */
	public int     size  = 0;
	/** The movie's data. */
	public long[]  data  = new long[BLOCKSIZE];
	/** The last 0-4 information bytes (the last information word). */
	public long    lastWord = 0;
	/** The size the last information word (0-4). */
	public int     lastSize = 0;

	/** The next datablock object. */
	private DataBlock next;
	
	/** Constructs a <code>Datablock</code> without data. */
	public DataBlock(){
		super();
	}

	/** 
	 * Constructs an empty <code>Datablock</code> as a continue of the existed <code>Datablock</code>.
	 * @param prevBlock the previous <code>Datablock</code>
	 **/
	public DataBlock(DataBlock prevBlock){
		super();
		prevBlock.setNext(this);
		prevBlock.close();
		setSize(prevBlock.lastSize);
		setFirstWord(prevBlock.lastWord);
	}

	/**
	 * Returns <tt>true</tt> if this data block has the next data block as a continue of it's data.   
	 * @return <tt>true</tt> if this data block has the next data block as a continue of it's data.
	 */
	public boolean hasNext(){
		return next != null;
	}

	/**
	 * Sets the first information word. 
	 * @param word the first information word.
	 */
	public void setFirstWord(long word) {
		data[0] = word;
	}

	/**
	 * Sets the next <code>Datablock</code> as a continue of this data block. 
	 * @param nextBlock the next <code>Datablock</code> object.
	 */
	public void setNext(DataBlock nextBlock){
		next = nextBlock;
	}

	/**
	 * Gets the next <code>Datablock</code> object.
	 * @return the next <code>Datablock</code> object.
	 */
	public DataBlock getNext(){
		return next;
	}

	/**
	 * Sets the size of this data block.
	 * @param size the size of this data block.
	 */
	public void setSize(int size) {
		this.size = size;
	}

	/**
	 * Returns <tt>true</tt> if this data block is full and can't store more data bytes.
	 * @return <tt>true</tt> if this data block is full.
	 */
	public boolean isFull(){
		return size >> 3 == BLOCKSIZE;
	}

	/**
	 * Puts a byte into this data block.
	 * @param Byte the byte of the movie's data.
	 */
	public void write(int Byte){
		int index = size >> 3;
		data[index] = data[index] << 8 | Byte;
		size++;
	}

	/**
	 * Closes this data block.
	 * Calculates the last information word (last 0-3 bytes of the information) and it's length.
	 */
	public void close(){
		if((size & 7) != 0) {
			int lastIndex = size >> 3;
			lastWord = data[lastIndex];
			lastSize = size & 7;
			size = size - lastSize;
		}
	}

}